package com.chenggong.demo.activity

import android.app.Activity
import android.content.res.Resources
import android.view.LayoutInflater
import android.view.View
import android.widget.RelativeLayout
import android.widget.TextView
import com.chenggong.demo.R
import com.chenggong.demo.activity.dialog.SafeDialog
import com.fengsu.baselib.permission.AuthorityEntity


object ShowDialog {
    /**
     * 授权
     */
    fun showAuthority(
            context: Activity,
            authorList: ArrayList<AuthorityEntity>,
            open: () -> Unit,
            cancel: (()->Unit)? = null
    ) {
        val dialog = SafeDialog(context, R.style.dialog_style)
        val view = LayoutInflater.from(context).inflate(R.layout.dialog_show_auth, null)
        val rlCamera = view.findViewById<RelativeLayout>(R.id.rl_camera)
        val rlPhoto = view.findViewById<RelativeLayout>(R.id.rl_photo)
        val btnCancel = view.findViewById<TextView>(R.id.btn_cancel)
        val btnAuth = view.findViewById<TextView>(R.id.btn_auth)

        val tvAllowCameraContent = view.findViewById<TextView>(R.id.tv_allow_camera_content)
        val tvAllowPhotoContent = view.findViewById<TextView>(R.id.tv_allow_photo_content)

        for (auth in authorList) {
            when (auth.name) {
                AuthorityEntity.AuthorityName.EXTERNAL_STORAGE -> {
                    tvAllowPhotoContent.text = auth.authorityContent
                    if (auth.hasAuthority) rlPhoto.visibility = View.GONE else rlPhoto.visibility =
                            View.VISIBLE
                }


                AuthorityEntity.AuthorityName.CAMERA -> {
                    tvAllowCameraContent.text = auth.authorityContent
                    if (auth.hasAuthority) rlCamera.visibility =
                            View.GONE else rlCamera.visibility = View.VISIBLE
                }
            }
        }
        btnCancel.setOnClickListener {
            cancel?.invoke()
            dialog.dismiss()

        }
        btnAuth.setOnClickListener {
            open.invoke()
            dialog.dismiss()
        }
        dialog.setCancelable(false)
        dialog.setContentView(view)
        dialog.show()
        // 将对话框的大小按屏幕大小的百分比设置
        val lp = dialog.window!!.attributes
        lp.width = (Resources.getSystem().displayMetrics.widthPixels * 0.8).toInt() //设置宽度
        dialog.window!!.attributes = lp
    }
}